package com.hxci.web.controller;

import com.hxci.web.bean.Conn;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RentServlet extends HttpServlet {
    ServletConfig servletConfig;
    @Override
    public void init(ServletConfig config) throws ServletException {
        servletConfig=config;

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String actionType = req.getParameter("actionType");
        switch (actionType) {
            case "rent_update":
                do_update(req, resp);
                break;
            case "rent_add":
                do_add(req, resp);
                break;
            default:
                break;
        }
    }

    private void do_update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");
        String Borrowingid = req.getParameter("Borrowingid");//借阅人id
        String readername = req.getParameter("readername");//借阅人姓名
        String bookid = req.getParameter("bookid");//书籍id
        String bookname = req.getParameter("bookname");//书籍名
        String Borrowingtime = req.getParameter("Borrowingtime");//借阅时间

        String returntime = req.getParameter("returntime");//预计归还时间
        String phone = req.getParameter("phone");//联系方式

        String sql = "UPDATE borrowings SET readername = ?," +
                "bookid = ?," +
                "bookname = ?," +
                "Borrowingtime = ?," +
                "returntime = ?," +
                "phone = ? where Borrowingid = ?";
        List<Object> params = new ArrayList<>();
        params.add(readername);
        params.add(bookid);
        params.add(bookname);
        params.add(Borrowingtime);
        params.add(returntime);
        params.add(phone);
        params.add(Borrowingid);

        Conn c=new Conn(realPath);
        c.doQuery(sql,params);
        resp.sendRedirect("rent.jsp");

    }

    protected void do_add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
//        ServletConfig
//        它封装了 Web 容器（如 Tomcat）为该 Servlet 提供的初始化配置信息，例如：
//        Servlet 的初始化参数（<init-param>）
//        Servlet 的名称（<servlet-name>）
//        关联的 ServletContext 对象
//                ServletContext
//        代表 Web 应用程序的上下文，是整个 Web 应用的全局配置和共享资源的访问入口。
//        所有 Servlet 共享同一个 ServletContext 实例，它提供了以下功能：
//        获取全局初始化参数（<context-param>）
//        跨 Servlet 共享数据（通过 setAttribute() 和 getAttribute()）
//        访问 Web 应用的资源（如文件路径、类加载器）
//        监听 Web 应用事件（如启动、关闭）
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");

        String readername = req.getParameter("readername");
        String bookid = req.getParameter("bookid");
        String bookname = req.getParameter("bookname");
        String Borrowingtime = req.getParameter("Borrowingtime");
        String returntime = req.getParameter("returntime");
        String status ="已借阅";
        String phone = req.getParameter("phone");

        String sql = "INSERT INTO borrowings ( readername , bookid, bookname,Borrowingtime ,returntime ,status,phone)" +
                "VALUES" +
                "( ?,?,?,?,?,?,?);";
        List<Object> params = new ArrayList<>();
        params.add(readername);
        params.add(bookid);
        params.add(bookname);
        params.add(Borrowingtime);
        params.add(returntime);
        params.add(status);
        params.add(phone);

        Conn c=new Conn(realPath);
        c.doQuery(sql,params);

        resp.sendRedirect("rent.jsp");
    }

}
